package com.swxc.smartorder.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.swxc.smartorder.R;
import com.swxc.smartorder.entity.ImageEntity;
import com.swxc.smartorder.entity.Record;
import com.swxc.smartorder.ui.activity.PreviewActivity;
import com.swxc.smartorder.utils.DateUtil;
import com.swxc.smartorder.utils.DisplayUtil;
import com.swxc.smartorder.utils.ImageDisplayUtil;
import com.swxc.smartorder.widget.RoundImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author   Administrator
 * Date     2018/3/30
 * Desc     eg
 *
 * @author Administrator
 */
public class AdapterRecord extends BaseQuickAdapter<Record, BaseViewHolder> {


    public AdapterRecord(@Nullable List<Record> data) {
        super(R.layout.item_record, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, final Record item) {
        TextView textView = helper.getView(R.id.tv_flag);
        ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
        int longPx = DisplayUtil.INSTANCE.dip2px(mContext, 10.0f);
        int shortPx = DisplayUtil.INSTANCE.dip2px(mContext, 6.0f);
        if (helper.getAdapterPosition() == 1) {
            layoutParams.height = longPx;
            textView.setLayoutParams(layoutParams);
        } else {
            layoutParams.height = shortPx;
            textView.setLayoutParams(layoutParams);
        }
        RecyclerView rvImages = helper.getView(R.id.rv_image);
        RoundImageView roundImageView = helper.getView(R.id.iv_avatar);
        helper.setText(R.id.tv_name_adapter, item.getStaffName());
        helper.setText(R.id.tv_content_adapter, item.getContent());
        ImageDisplayUtil.showImageView(mContext, item.getHeadImage(), roundImageView);
        helper.setText(R.id.tv_time_adapter, DateUtil.INSTANCE.getTimeFormatString(item.getCreateTime()));

        if (item.getImages().size() > 0) {
            AdapterReplyImage adapterReplyImage = new AdapterReplyImage(item.getImages());
            rvImages.setVisibility(View.VISIBLE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvImages.setLayoutManager(linearLayoutManager);
            rvImages.setAdapter(adapterReplyImage);

            final ArrayList<String> imageList = new ArrayList<>();


            for (ImageEntity imageItem : item.getImages()) {

                imageList.add(imageItem.getAccessUrl());
            }
            adapterReplyImage.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt("index", position);
                    bundle.putStringArrayList("images", imageList);
                    intent.putExtras(bundle);
                    intent.setClass(mContext, PreviewActivity.class);
                    mContext.startActivity(intent);
                }
            });

        } else {
            rvImages.setVisibility(View.GONE);
        }
    }


}
